﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Kanban.Entity;
using Kanban.Business;

namespace Kanban.Api.Controllers
{
    
    public class QuadrosController : ApiController
    {
        [Authorize]
        public List<Quadro> Get()
        {
            var business = new QuadroBusiness();
            List<Quadro> quadros = business.Listar();
            return quadros;
        }

        [Authorize]
        public Quadro Get(int id)
        {
            var business = new QuadroBusiness();
            Quadro quadro = business.ObterPorId(id);

            return quadro;
        }

        [Authorize]
        public List<Quadro> Get(string nomeUsuario)
        {
            var business = new QuadroBusiness();
            List<Quadro> quadros = business.ListarPorUsuario(nomeUsuario, false);
            return quadros;
        }

        // ATUALIZAR
        [Authorize]
        public Quadro Put([FromBody]Quadro quadro)
        {
            var business = new QuadroBusiness();
            business.Atualizar(quadro);
            return quadro;
        }

        // ARQUIVAR
        [Authorize]
        [HttpPut]
        public Quadro Arquivar(int id)
        {
            var business = new QuadroBusiness();
            Quadro quadro = business.ObterPorId(id);

            if (quadro == null)
            {
                var response = this.Request.CreateResponse(HttpStatusCode.NotFound, new { ExceptionMessage = "Quadro não encontrado." });
                throw new HttpResponseException(response);                
            }

            quadro.Arquivado = true;
            business.Atualizar(quadro);
            return quadro;
        }

        // ADICIONAR
        [Authorize]
        public Quadro Post(Quadro quadro)
        {
            var business = new QuadroBusiness();
            business.Adicionar(quadro);
            return quadro;
        }

        [Authorize]
        public Quadro Delete(int id)
        {
            var business = new QuadroBusiness();
            var quadro = business.ObterPorId(id);

            if (quadro == null)
            {
                var response = this.Request.CreateResponse(HttpStatusCode.NotFound, new { ExceptionMessage = "Quadro não encontrado." });
                throw new HttpResponseException(response);
            }

            quadro.Excluido = true;
            business.Atualizar(quadro);
            return quadro;
        }
    }
}
